﻿using HT.Framework;
using TG.Timer;
using UnityEngine;

namespace TG
{
    public class GameManager
    {
        private static GameManager instance;

        public static GameManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new GameManager();
                }

                return instance;
            }
        }

        /// <summary>
        /// 当前数据
        /// </summary>
        public GameData gameData = new GameData();

        private int curSeconds;


        public LambPlayer Player;

        public GameData GetGameData()
        {
            return gameData;
        }

        public void InitCurLevelData()
        {
            gameData.InitGameConfigData();
            TimerManager.Instance.AddSecondsElapsed(SecondsElapsed);
        }

        private void SecondsElapsed()
        {
            gameData.LossOfVitality();
            gameData.LossOfTime();
        }

        public void BeginGame()
        {
            //创建主角羊羊
            CreatePayerLamb();
            //创建狼
            CreateWolf();
            //创建运输箱
            CreateTransportCase();
            //创建宝箱
            CreateTreasureBox();
            //创建其他
            CreateMapItems();
        }

        private void CreatePayerLamb()
        {
            var obj = Object.Instantiate(PoolItemInitManager.Instance.sheep);
            Player = obj.GetComponent<LambPlayer>();
            Player.Init(null);
            CameraManager.Instance.InitCamera(obj.transform);
        }

        private void CreateWolf()
        {
            if (MainGame.Instance.curGameConfig.gameType==1)return;
            
            int createNum = gameData.gameConfigData.wolf;
            int rate = MainGame.Instance.curGameConfig.difficulty;
            for (int i = 0; i < createNum * rate; i++)
            {
                MapCell lamb = Map.Instance.RandomCreateItem();
                //创建一片危险区域
                var dangerArea = Main.m_ObjectPool.Spawn("DangerArea");
                //创建一个狼
                var obj = Main.m_ObjectPool.Spawn(MapItemType.Wolf.ToString());
                lamb.SetData(MapItemType.Wolf);
                Wolf wolf = obj.GetComponent<EntityBase>() as Wolf;
                if (wolf != null)
                {
                    wolf.Init(lamb);
                    wolf.InitDangerArea(dangerArea);
                }

                gameData.AddEntity(wolf);
            }
        }

        private void CreateTransportCase()
        {
            if (MainGame.Instance.curGameConfig.gameType==1)return;
            int createNum = gameData.gameConfigData.GetNeedTransportCaseNum();
            for (int i = 0; i < createNum; i++)
            {
                MapCell lamb = Map.Instance.RandomCreateItem();
                //创建运输箱
                var obj = Main.m_ObjectPool.Spawn(MapItemType.TransportCase.ToString());

                lamb.SetData(MapItemType.TransportCase);

                var entity = obj.GetComponent<EntityBase>() as TransportCase;
                if (entity != null)
                {
                    TransportCaseType transportCase = TransportCaseType.None;
                    int[] transportCaseTypeNum = gameData.gameConfigData.GetTransportCase();
                    if (i >= transportCaseTypeNum[0] && i < transportCaseTypeNum[0] + transportCaseTypeNum[1])
                        transportCase = TransportCaseType.CabbageBox;
                    else if (i >= transportCaseTypeNum[0] + transportCaseTypeNum[1])
                        transportCase = TransportCaseType.PeachBox;

                    entity.Init(lamb, transportCase);
                    gameData.AddEntity(entity);
                }
            }
        }

        private void CreateTreasureBox()
        {
            if (MainGame.Instance.curGameConfig.gameType==1)return;
            int createNum = gameData.gameConfigData.GetNeedTreasureBoxNum();
            for (int i = 0; i < createNum; i++)
            {
                MapCell lamb = Map.Instance.RandomCreateItem();
                //创建宝箱
                var obj = Main.m_ObjectPool.Spawn(MapItemType.TreasureBox.ToString());

                lamb.SetData(MapItemType.TreasureBox);

                var entity = obj.GetComponent<EntityBase>() as TreasureBox;
                if (entity != null)
                {
                    TreasureBoxType treasureBoxType = TreasureBoxType.VacuumCleaner;
                    int[] treasureBoxTypeNum = gameData.gameConfigData.GetTreasureBox();
                    if (i >= treasureBoxTypeNum[0])
                        treasureBoxType = TreasureBoxType.Cleat;

                    entity.Init(lamb, treasureBoxType);
                    gameData.AddEntity(entity);
                }
            }
        }

        private void CreateMapItems()
        {
            for (MapItemType i = MapItemType.Pumpkin; i < MapItemType.Max; i++)
            {
                int maxNum = Default.GetMaxNumByType(i);
                for (int j = 0; j < maxNum; j++)
                {
                    MapCell lamb = Map.Instance.RandomCreateItem();
                    lamb.SetData(i);
                    //创建一个蔬菜
                    var obj = Main.m_ObjectPool.Spawn(i.ToString());

                    var entity = obj.GetComponent<EntityBase>();
                    entity.Init(lamb);
                    gameData.AddEntity(entity);
                }
            }
        }

        /// <summary>
        /// 吃一个蔬菜
        /// </summary>
        /// <param name="index"></param>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        public void ResetItem(int index, MapItemType type, EntityBase obj)
        {
            gameData.EatEntity(obj);
            ItemJoinPool(index, type, obj);
            CreateMapItems(type);
        }

        /// <summary>
        /// 一个实体加入对象池
        /// </summary>
        /// <param name="index"></param>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        public void ItemJoinPool(int index, MapItemType type, EntityBase entity)
        {
            Map.Instance.RemoveItem(index);
            Main.m_ObjectPool.Despawn(type.ToString(), entity.gameObject);
            gameData.RemoveEntity(entity);
        }

        /// <summary>
        /// 创建一个新的
        /// </summary>
        /// <param name="type"></param>
        public void CreateMapItems(MapItemType type)
        {
            MapCell lamb = Map.Instance.RandomCreateItem();
            lamb.SetData(type);
            //创建一个蔬菜
            var obj = Main.m_ObjectPool.Spawn(type.ToString());
            var entity = obj.GetComponent<EntityBase>();
            entity.Init(lamb);
            gameData.AddEntity(entity);
        }

        /// <summary>
        /// 重新开始
        /// </summary>
        public void GameReset()
        {
            Debug.Log($"关卡重置！！！");
            gameData.RemoveAllEntity();
            Map.Instance.Clear();
            gameData.Reset();
            //创建狼
            CreateWolf();
            //创建运输箱
            CreateTransportCase();
            //创建宝箱
            CreateTreasureBox();
            //创建其他
            CreateMapItems();
            //羊初始化
            Player.Reset();
        }

        /// <summary>
        /// 切换关卡
        /// </summary>
        public void ChangeLevel()
        {
            if (gameData.levelID >= 3)
            {
                Debug.Log($"最后一关 游戏结束");
                MainGame.Instance.ExitScene();
                return;
            }

            Debug.Log($"升级切换关卡！！！");
            gameData.RemoveAllEntity();
            Map.Instance.Clear();
            gameData.LevelUp();
            //创建狼
            CreateWolf();
            //创建运输箱
            CreateTransportCase();
            //创建宝箱
            CreateTreasureBox();
            //创建其他
            CreateMapItems();
            //羊初始化
            Player.Reset();
        }
    }
}